from feiyan import get_data_dict
import time
import os
from pyecharts.charts import Bar
from pyecharts import options as opts
from pyecharts.globals import ThemeType
from flask import Flask, render_template
from jinja2 import Markup, Environment, FileSystemLoader
from pyecharts.globals import CurrentConfig



def generate_bar() -> Bar:
    num, province ,province_dict= get_data_dict()
    t = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    tile = "全国各省(市)确诊人数 (截至到:" + t + ")"

    bar = (
        Bar(init_opts=opts.InitOpts(width='1250px',height='600px',theme=ThemeType.ESSOS))
        .set_global_opts(title_opts=opts.TitleOpts(title="新型冠状病毒", subtitle=tile),
        xaxis_opts=opts.AxisOpts(name='省份',splitline_opts=opts.SplitLineOpts(is_show=False),axislabel_opts={'rotate':270}),
        yaxis_opts=opts.AxisOpts(axistick_opts=opts.AxisTickOpts(is_show=True),splitline_opts=opts.SplitLineOpts(is_show=True))
        )
        .add_xaxis(province)
        .add_yaxis("确诊人数", num)
    )

    return bar

CurrentConfig.GLOBAL_ENV = Environment(loader=FileSystemLoader("./templates"))

app = Flask(__name__, static_folder="templates")


@app.route('/')
def show_bar():
   bar = generate_bar()
   bar.render(os.getcwd()+"\\mycharts.html")

   return Markup(bar.render_embed())

if __name__ == "__main__":
    app.run(host='0.0.0.0',port=8000)